package com.sykj.why.controller.front.relevantCertificate;

import com.sykj.why.exception.WhyException;
import com.sykj.why.handler.Response;
import com.sykj.why.mongo.document.entity.RelevantCertificateT;
import com.sykj.why.mongo.enums.exception.ResultEnum;
import com.sykj.why.mongo.service.RelevantCertificateTService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * @author Eleven
 * @date 2019/7/5
 * @desc 相关证明材料控制访问层
 */

@RestController
@RequestMapping("/front/relevantCertificate")
@Slf4j
@Api(value = "/front/relevantCertificate",description = "相关证明材料控制访问层")
public class RelevantCertificateController {

    @Autowired
    private RelevantCertificateTService relevantCertificateTService;

    /**
     * 保存相关证明材料路径
     * @param relevantCertificateT
     * @return
     */
    @RequestMapping(value = "/saveFileUrl",method = RequestMethod.POST)
    @ApiOperation(value = "/saveFileUrl",notes = "保存相关证明材料路径")
    public ResponseEntity saveFileUrl(@RequestBody RelevantCertificateT relevantCertificateT){
        try {
            String id = relevantCertificateTService.saveRelevantCertificate(relevantCertificateT);
            //保存之后将id返回
            return Response.success("保存成功",id);
        } catch (WhyException e) {
            return Response.serverError(e.getMessage(), e.getCode());
        } catch (Exception e) {
            log.error("保存相关证明材料路径出现异常，{}", e.getMessage());
            return Response.serverError(ResultEnum.UNERROR.getMessage(), ResultEnum.UNERROR.getCode());
        }
    }

    /**
     * 删除相关证明材料
     * @param id
     * @return
     */
    @RequestMapping(value = "/remove/{id}",method = RequestMethod.GET)
    @ApiOperation(value = "/remove/{id}",notes = "删除相关证明材料")
    public ResponseEntity remove(@PathVariable String id){
        try {
            relevantCertificateTService.removeRelevantCertificate(id);
            return Response.success("删除成功");
        } catch (WhyException e) {
            return Response.serverError(e.getMessage(), e.getCode());
        } catch (Exception e) {
            log.error("删除相关证明材料出现未知异常，{}", e.getMessage());
            return Response.serverError(ResultEnum.UNERROR.getMessage(), ResultEnum.UNERROR.getCode());
        }
    }

    /**
     * 修改相关证明材料路径
     * @param relevantCertificateT
     * @return
     */
    @RequestMapping(value = "/updateFileUrl",method = RequestMethod.POST)
    @ApiOperation(value = "/updateFileUrl",notes = "修改相关证明材料路径")
    public ResponseEntity updateFileUrl(@RequestBody RelevantCertificateT relevantCertificateT){
        try {
            relevantCertificateTService.updateRelevantCertificate(relevantCertificateT);
            return Response.success("修改成功");
        } catch (WhyException e) {
            return Response.serverError(e.getMessage(), e.getCode());
        } catch (Exception e) {
            log.error("修改相关证明材料路径出现异常，{}", e.getMessage());
            return Response.serverError(ResultEnum.UNERROR.getMessage(), ResultEnum.UNERROR.getCode());
        }
    }

}
